﻿using SmartMobile.Data;
using SMARTMobile.Models.Local;
using SMARTMobile.WinPhone;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SMARTMobile.Common;
using SMARTMobile.Services.SMARTMobileServiceReference;
using SMARTMobile.Models;
using SMARTMobile.Models.UI;

namespace SMARTMobile.ViewModels
{
    public partial class NhapDuLieuViewModel : ViewModelBase
    {
        private PriceRange _selectedPriceRange;
        private ObservableCollection<PriceRange> _priceRangeObservable;
        private ObservableCollection<PriceRangeContributionExtend> _priceRangeContributionObservable;
        private ObservableCollection<PhoneVendor> _phoneVenderObservable;
        private ObservableCollection<PhonevenforVolumeExtend> _phonevendorVolumeObservable;
        private ObservableCollection<OutletVendor> _outletVenderObservable;
        private ObservableCollection<OutltVendorContributionExtend> _outletVendorContributionObserable;
        private ObservableCollection<OutletContactModel> _outletContactObservable;
        private ObservableCollection<OutDoorType> _outdoorTypeObservable;
        private ObservableCollection<CurrentOutDoorDisplayExtend> _currentOutDoorDisplayObservable;
        private ObservableCollection<CurrentInDoorDisplayExtend> _currentInDoorDoorDisplayObservable;
        private ObservableCollection<OutletPGInformationExtend> _outetPGInformationObservable;
        private ObservableCollection<OutletNokiaPGExtend> _outletNokiaPGObservable;
        private ObservableCollection<OutletNokiaMerchandisingPotentialExtend> _outletNokiaMerchandisingPotentialObservable;
        private ObservableCollection<Outlet> _outletObservable;
        private ObservableCollection<OutletContactModel> _contactInforObservable;
        private ObservableCollection<NokiaPG> _nokiaPGObservable;
        private ObservableCollection<OutletNokiaMerchandisingType> _outletNokiaMerchandisingTypeObservable;
        private ObservableCollection<OutletAdvanceInforListing> _outletAdvanceInforListingObservable;
        private ObservableCollection<OutletAdvanceInforTable> _outletAdvanceInforTablebservable; 

        private Dictionary<string, string> _province;

        private SmartMobileDBContext _smartMobileContext;
        private ReloadDataSmartPhoneSoapClient _proxy;
        private string _imageName = string.Empty;
        private CurrentOutDoorDisplayExtend _selectedOutDoorDisplay;
        public NhapDuLieuViewModel()
        {
            this.CreateConnection();
            this.GetOutletAdvanceInforListing();
            //this.GetPhoneVender();

            //this.GetPriceRangeContribution();
            //this.GetPhonevendorVolume();
            //this.GetOutltVendorContribution();
            //this.GetCurrentOutDoorDisplay();
            //this.GetCurrentInDoorDoorDisplay();
            //this.GetOutletPGInformation();
            //this.GetOutletNokiaPG();
            //this.GetOutletNokiaMerchandisingPotential();
            //this.GetContactInfor();
            //this.GetOutlet();

            this.ExitCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteThoatCommand
            };

            this.SaveCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteSaveCommand
            };

            this._proxy = new ReloadDataSmartPhoneSoapClient(GlobalInformation.Binding, GlobalInformation.Endpoint);
        }
        #region Command

        public ICommandRoute ExitCommand
        {
            get;

            private set;
        }

        public ICommandRoute SaveCommand
        {
            get;

            private set;
        }
        #endregion
        #region Properties

        public ObservableCollection<OutletAdvanceInforListing> OutletAdvanceInforListingObservable
        {
            get { return this._outletAdvanceInforListingObservable; }
            set
            {
                this._outletAdvanceInforListingObservable = value;
                base.RaisePropertyChanged("OutletAdvanceInforListingObservable");
            }
        }

        public ObservableCollection<OutletAdvanceInforTable> OutletAdvanceInforTableObservable
        {
            get { return this._outletAdvanceInforTablebservable; }
            set
            {
                this._outletAdvanceInforTablebservable = value;
                base.RaisePropertyChanged("OutletAdvanceInforTablebservable");
            }
        }

        public ReloadDataSmartPhoneSoapClient Proxy
        {
            get
            {
                if (_proxy == null)
                {
                    _proxy = new ReloadDataSmartPhoneSoapClient(GlobalInformation.Binding, GlobalInformation.Endpoint);
                }

                return _proxy;
            }
            set { _proxy = value; }
        }

        public Dictionary<string, string> InDoorTypeList
        {
            get
            {
                var inDoorList = this._smartMobileContext.InDoorType.ToList();
                Dictionary<string, string> inDooreDic = new Dictionary<string, string>();
                foreach (var item in inDoorList)
                {
                    inDooreDic.Add(item.InDoorTypeCode, item.Description);
                }
                return inDooreDic;
            }
        }

        public Dictionary<string, string> OutDoorTypeList
        {
            get
            {
                var outDoorList = this._smartMobileContext.OutDoorType.ToList();
                Dictionary<string, string> outDooreDic = new Dictionary<string, string>();
                foreach (var item in outDoorList)
                {
                    outDooreDic.Add(item.OutDoorTypeCode, item.Description);
                }
                return outDooreDic;
            }
        }

        public Dictionary<string, string> PrivinceList
        {
            get
            {
                var provinceList = this._smartMobileContext.PROVINCE.ToList();
                Dictionary<string, string> provinceDic = new Dictionary<string, string>();
                foreach (var item in provinceList)
                {
                    provinceDic.Add(item.ProvinceKey, item.ProvinceName);
                }
                return provinceDic;
            }
        }

        public Dictionary<string, string> PhoneVenderList
        {
            get
            {
                var phoneVenderList = this._smartMobileContext.PhoneVendor.ToList();
                Dictionary<string, string> phoneVenderDic = new Dictionary<string, string>();
                foreach (var item in phoneVenderList)
                {
                    phoneVenderDic.Add(item.PhoneVendorCode, item.PhoneVendorName);
                }

                return phoneVenderDic;
            }
        }

        public ObservableCollection<OutletContactModel> ContactInforObservable
        {
            get { return this._contactInforObservable; }
            set
            {
                this._contactInforObservable = value;
                base.RaisePropertyChanged("ContactInforObservable");
            }
        }

        public ObservableCollection<OutletNokiaMerchandisingType> OutletNokiaMerchandisingTypeObservable
        {
            get { return this._outletNokiaMerchandisingTypeObservable; }
            set
            {
                this._outletNokiaMerchandisingTypeObservable = value;
                base.RaisePropertyChanged("OutletNokiaMerchandisingTypeObservable");
            }
        }

        public ObservableCollection<Outlet> OutletObservable
        {
            get { return this._outletObservable; }
            set
            {
                this._outletObservable = value;
                base.RaisePropertyChanged("OutletObservable");
            }
        }

        public ObservableCollection<OutletNokiaMerchandisingPotentialExtend> OutletNokiaMerchandisingPotentialObservable
        {
            get { return this._outletNokiaMerchandisingPotentialObservable; }
            set
            {
                this._outletNokiaMerchandisingPotentialObservable = value;
                base.RaisePropertyChanged("OutletNokiaMerchandisingPotentialObservable");
            }
        }

        public ObservableCollection<OutletNokiaPGExtend> OutletNokiaPGObservable
        {
            get { return this._outletNokiaPGObservable; }
            set
            {
                this._outletNokiaPGObservable = value;
                base.RaisePropertyChanged("OutletNokiaPGObservable");
            }
        }

        public ObservableCollection<OutletPGInformationExtend> OutetPGInformationObservable
        {
            get { return this._outetPGInformationObservable; }
            set
            {
                this._outetPGInformationObservable = value;
                base.RaisePropertyChanged("OutetPGInformationObservable");
            }
        }

        public ObservableCollection<CurrentInDoorDisplayExtend> CurrentInDoorDoorDisplayObservable
        {
            get { return this._currentInDoorDoorDisplayObservable; }
            set
            {
                this._currentInDoorDoorDisplayObservable = value;
                base.RaisePropertyChanged("CurrentInDoorDoorDisplayObservable");
            }
        }

        public ObservableCollection<CurrentOutDoorDisplayExtend> CurrentOutDoorDisplayObservable
        {
            get { return this._currentOutDoorDisplayObservable; }
            set
            {
                this._currentOutDoorDisplayObservable = value;
                base.RaisePropertyChanged("CurrentOutDoorDisplayObservable");
            }
        }

        public ObservableCollection<OutletContactModel> OutletContactObservable
        {
            get { return this._outletContactObservable; }
            set
            {
                this._outletContactObservable = value;
                base.RaisePropertyChanged("OutletContactObservable");
            }
        }

        public ObservableCollection<OutltVendorContributionExtend> OutletVendorContributionObserable
        {
            get { return this._outletVendorContributionObserable; }
            set
            {
                this._outletVendorContributionObserable = value;
                base.RaisePropertyChanged("OutletVendorContributionObserable");
            }
        }

        public ObservableCollection<OutletVendor> OutletVenderObservable
        {
            get { return this._outletVenderObservable; }
            set
            {
                this._outletVenderObservable = value;
                base.RaisePropertyChanged("OutletVenderObservable");
            }
        }

        public ObservableCollection<PhonevenforVolumeExtend> PhonevendorVolumeObservable
        {
            get { return this._phonevendorVolumeObservable; }
            set
            {
                this._phonevendorVolumeObservable = value;
                base.RaisePropertyChanged("PhonevendorVolumeObservable");
            }
        }

        public ObservableCollection<PhoneVendor> PhoneVendorObservable
        {
            get { return this._phoneVenderObservable; }
            set
            {
                this._phoneVenderObservable = value;
                base.RaisePropertyChanged("PhoneVenderObservable");
            }
        }

        public ObservableCollection<PriceRangeContributionExtend> PriceRangeContributionObservable
        {
            get { return this._priceRangeContributionObservable; }
            set
            {
                this._priceRangeContributionObservable = value;
                base.RaisePropertyChanged("PriceRangeContributionObservable");
            }
        }

        public PriceRange SelectedPriceRange
        {
            get
            {
                return this._selectedPriceRange;
            }
            set
            {
                this._selectedPriceRange = value;
                RaisePropertyChanged("SelectedPriceRange");
            }
        }

        public ObservableCollection<PriceRange> PriceRangeObservable
        {
            get { return this._priceRangeObservable; }
            set
            {
                this._priceRangeObservable = value;
                base.RaisePropertyChanged("PriceRangeObservable");
            }
        }

        public ObservableCollection<NokiaPG> NokiaPGObservable
        {
            get { return this._nokiaPGObservable; }
            set
            {
                this._nokiaPGObservable = value;
                base.RaisePropertyChanged("NokiaPGObservable");
            }
        }

        public string ImageName
        {
            get { return _imageName; }
            set { _imageName = value; }
        }

        public CurrentOutDoorDisplayExtend SelectedOutDoorDisplay
        {
            get { return this._selectedOutDoorDisplay; }
            set
            {
                this._selectedOutDoorDisplay = value;
                base.RaisePropertyChanged("SelectedOutDoorDisplay");
            }
        }

        #endregion

        private void ExecuteSaveCommand(object obj)
        {
            base.IsBusy = true;

            // Delete all Nokia database reference
            this._smartMobileContext.PhonevendorVolume.DeleteAllOnSubmit(this._smartMobileContext.PhonevendorVolume.Where(x => x.CustomerKey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList());
            this._smartMobileContext.PriceRangeContribution.DeleteAllOnSubmit(this._smartMobileContext.PriceRangeContribution.Where(x => x.CustomerKey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList());
            this._smartMobileContext.OutletNokiaPG.DeleteAllOnSubmit(this._smartMobileContext.OutletNokiaPG.Where(x => x.Customerkey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList());
            this._smartMobileContext.OutetPGInformation.DeleteAllOnSubmit(this._smartMobileContext.OutetPGInformation.Where(x => x.Customerkey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList());
            this._smartMobileContext.OutletVendorContribution.DeleteAllOnSubmit(this._smartMobileContext.OutletVendorContribution.Where(x => x.CustomerKey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList());
            this._smartMobileContext.OutletContact.DeleteAllOnSubmit(this._smartMobileContext.OutletContact.Where(x => x.CustomerKey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList());
            this._smartMobileContext.CurrentInDoorDoorDisplay.DeleteAllOnSubmit(this._smartMobileContext.CurrentInDoorDoorDisplay.Where(x => x.CustomerKey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList());
            this._smartMobileContext.CurrentOutDoorDisplay.DeleteAllOnSubmit(this._smartMobileContext.CurrentOutDoorDisplay.Where(x => x.Customerkey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList());
            this._smartMobileContext.OutletNokiaMerchandisingPotential.DeleteAllOnSubmit(this._smartMobileContext.OutletNokiaMerchandisingPotential.Where(x => x.Customerkey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList());

            this._smartMobileContext.SubmitChanges();

            // Save PhoneVender
            List<PhonevendorVolume> phoneVenderList = new List<PhonevendorVolume>();
            foreach (var phoneVendor in this.PhonevendorVolumeObservable)
            {
                PhonevendorVolume vender = new PhonevendorVolume
                {
                    ID_PK = phoneVendor.ID_PK,
                    CustomerKey = phoneVendor.CustomerKey,
                    PhoneVendorCode = phoneVendor.PhoneVendorCode,
                    RetailVolume = phoneVendor.RetailVolume,
                    RetialValue = phoneVendor.RetailVolume,
                    WhosesaleValue = phoneVendor.WhosesaleValue,
                    WhosesaleVolume = phoneVendor.WhosesaleVolume
                };

                phoneVenderList.Add(vender);
            }

            this._smartMobileContext.PhonevendorVolume.InsertAllOnSubmit(phoneVenderList);

            // Save PriceRange
            List<SmartMobile.Data.PriceRangeContribution> priceRangeList = new List<SmartMobile.Data.PriceRangeContribution>();
            foreach (var priceRange in this.PriceRangeContributionObservable)
            {
                PriceRangeContribution item = new PriceRangeContribution
                {
                    ID_PK = priceRange.ID_PK,
                    CustomerKey = priceRange.CustomerKey,
                    PriceRangeCode = priceRange.PriceRangeCode,
                    ContributionByValue = priceRange.ContributionByValue,
                    ContributionByVolume = priceRange.ContributionByVolume
                };
                priceRangeList.Add(item);
            }

            this._smartMobileContext.PriceRangeContribution.InsertAllOnSubmit(priceRangeList);

            // Save OutletNokiaPG
            List<OutletNokiaPG> outletNokiaPGList = new List<OutletNokiaPG>();
            foreach (var nokiaPG in this.OutletNokiaPGObservable)
            {
                OutletNokiaPG nokiaPGItem = new OutletNokiaPG
                {
                    ID_PK = nokiaPG.ID_PK,
                    Customerkey = nokiaPG.Customerkey,
                    PGCode = nokiaPG.PGCode,
                    StartWorkingTime = nokiaPG.StartWorkingTime,
                    EndWorkingTime = nokiaPG.EndWorkingTime
                };
                outletNokiaPGList.Add(nokiaPGItem);
            }
            this._smartMobileContext.OutletNokiaPG.InsertAllOnSubmit(outletNokiaPGList);

            // Save OutletPGInformation
            List<OutetPGInformation> outletPGList = new List<OutetPGInformation>();
            foreach (var pgInformation in this.OutetPGInformationObservable)
            {
                OutetPGInformation outletPG = new OutetPGInformation
                {
                    ID_PK = pgInformation.ID_PK,
                    Customerkey = pgInformation.Customerkey,
                    PhoneVendorCode = pgInformation.PhoneVendorCode,
                    Qty = pgInformation.Qty
                };

                outletPGList.Add(outletPG);
            }
            this._smartMobileContext.OutetPGInformation.InsertAllOnSubmit(outletPGList);

            // Save OutltVendorContribution
            List<OutletVendorContribution> outletVenderList = new List<OutletVendorContribution>();
            foreach (var outletVendorContribution in this.OutletVendorContributionObserable)
            {
                OutletVendorContribution vender = new OutletVendorContribution
                {
                    ID_PK = outletVendorContribution.ID_PK,
                    CustomerKey = outletVendorContribution.CustomerKey,
                    OutletVendorKey = outletVendorContribution.OutletVendorKey,
                    ContributionAmt = outletVendorContribution.ContributionAmt
                };
                outletVenderList.Add(vender);
            }
            this._smartMobileContext.OutletVendorContribution.InsertAllOnSubmit(outletVenderList);

            // Save Contact Infor         
            if (this.ContactInforObservable != null)
            {
                List<OutletContact> outletContact = new List<OutletContact>();
                foreach (var contactInfor in this.ContactInforObservable)
                {

                    OutletContact contact = new OutletContact
                    {
                        ID_PK = Guid.NewGuid(),
                        CustomerKey = contactInfor.CustomerKey,
                        Email = contactInfor.Email,
                        Fullname = contactInfor.FullName,
                        Gender = ConvertGender(contactInfor.Gender),
                        Phone = contactInfor.Phone,
                        Role = contactInfor.Role,
                        Title = contactInfor.Title
                    };
                    outletContact.Add(contact);
                }

                this._smartMobileContext.OutletContact.InsertAllOnSubmit(outletContact);
            }

            // Save current out door
            List<CurrentOutDoorDisplay> currentOutDoorDisplay = new List<CurrentOutDoorDisplay>();
            foreach (var item in this.CurrentOutDoorDisplayObservable)
            {
                if (!string.IsNullOrEmpty(item.Picture))
                {
                    CurrentOutDoorDisplay outDoor = new CurrentOutDoorDisplay
                    {
                        ID_PK = Guid.NewGuid(),
                        Customerkey = GlobalInformation.SelectedTCARCUS.Customerkey,
                        PhoneVendorCode = item.PhoneVendorCode,
                        OutDoorTypeCode = item.OutDoorTypeCode,
                        Qty = 0
                    };

                    currentOutDoorDisplay.Add(outDoor);
                }
            }

            if (currentOutDoorDisplay.Any())
            {
                this._smartMobileContext.CurrentOutDoorDisplay.InsertAllOnSubmit(currentOutDoorDisplay);
            }

            // Save current in door
            List<CurrentInDoorDoorDisplay> currentInList = new List<CurrentInDoorDoorDisplay>();
            foreach (var item in this.CurrentInDoorDoorDisplayObservable)
            {
                if (!string.IsNullOrEmpty(item.Picture))
                {
                    CurrentInDoorDoorDisplay outDoor = new CurrentInDoorDoorDisplay
                   {
                       ID_PK = Guid.NewGuid(),
                       CustomerKey = GlobalInformation.SelectedTCARCUS.Customerkey,
                       PhoneVendorCode = item.PhoneVendorCode,
                       InDoorTypeCode = item.InDoorTypeCode,
                       Qty = "0"
                   };

                    currentInList.Add(outDoor);
                }
            }

            if (currentInList.Any())
            {
                this._smartMobileContext.CurrentInDoorDoorDisplay.InsertAllOnSubmit(currentInList);
            }

            // Save Outlet Nokia Merchandising Potential
            List<OutletNokiaMerchandisingPotential> merchandisingList = new List<OutletNokiaMerchandisingPotential>();
            foreach (var item in this.OutletNokiaMerchandisingPotentialObservable)
            {
                if (item.MerchandisingPicture != null)
                {
                    OutletNokiaMerchandisingPotential merchandising = new OutletNokiaMerchandisingPotential
                    {
                        ID_PK = Guid.NewGuid(),                        
                        Customerkey = GlobalInformation.SelectedTCARCUS.Customerkey,
                        MerchandisingCode = item.MerchandisingCode,         
                        CaptureTiming = DateTime.Now,                        
                    };

                    merchandisingList.Add(merchandising);
                }
            }

            if (merchandisingList.Any())
            {
                this._smartMobileContext.OutletNokiaMerchandisingPotential.InsertAllOnSubmit(merchandisingList);
            }


            this._smartMobileContext.SubmitChanges();

            base.IsBusy = false;
            this.SaveCommand.RaiseCanExecuteChanged(true);
            this.ShowMessage("Lưu thành công");

            GlobalInformation.SelectedTCARCUS = null;
            base.PageNavigation.Navigate("Main");
        }

        private void ExecuteThoatCommand(object commandParams)
        {
            this.ExitCommand.RaiseCanExecuteChanged(true);
            base.PageNavigation.Navigate("Main");
        }

        private void GetOutlet()
        {
            OutletObservable = new ObservableCollection<Outlet>();
            Outlet outlet = null;

            if (GlobalInformation.SelectedTCARCUS != null)
            {
                outlet = new Outlet
                {
                    ID_PK = Guid.NewGuid(),
                    Address = GlobalInformation.SelectedTCARCUS.Address,
                    BelongMD = true,
                    CityCode = GlobalInformation.SelectedTCARCUS.Town,
                    Customerkey = GlobalInformation.SelectedTCARCUS.Customerkey,
                    CustomerName = GlobalInformation.SelectedTCARCUS.Customername,
                    Email = string.Empty,
                    Fax = string.Empty,
                    GPSH = 0,
                    GPSV = 0,
                    Headquarterofchain = true,
                    NoOfSalesStaff = 0,
                    Operators = GlobalInformation.SelectedTCARCUS.Otype,
                    Phone = GlobalInformation.SelectedTCARCUS.Phone,
                    ProvinceCode = GlobalInformation.SelectedTCARCUS.Provice,
                    RetailChainCode = "",
                    ShopLong = 0,
                    ShopName = GlobalInformation.SelectedTCARCUS.ShopOwner,
                    ShopWidth = 0,
                    Status = true,
                    Street = GlobalInformation.SelectedTCARCUS.Street,
                    TownCode = GlobalInformation.SelectedTCARCUS.Town,
                    Ward = GlobalInformation.SelectedTCARCUS.Ward
                };
            }
            else
            {
                outlet = new Outlet
                {
                    ID_PK = Guid.NewGuid(),
                    Address = "",
                    BelongMD = true,
                    CityCode = "",
                    Customerkey = "",
                    CustomerName = "",
                    Email = "",
                    Fax = "",
                    GPSH = 0,
                    GPSV = 0,
                    Headquarterofchain = true,
                    NoOfSalesStaff = 0,
                    Operators = "",
                    Phone = "",
                    ProvinceCode = "HCM",
                    RetailChainCode = "",
                    ShopLong = 0,
                    ShopName = "",
                    ShopWidth = 0,
                    Status = true,
                    Street = "",
                    TownCode = "",
                    Ward = ""
                };
            }


            this.OutletObservable.Add(outlet);
        }

        private void CreateConnection()
        {
            this._smartMobileContext = new SmartMobileDBContext(SmartMobileDBContext.ConnectionString);
            this._smartMobileContext.CreateIfNotExists();
            this._smartMobileContext.LogDebug = true;
        }

        private void GetContactInfor()
        {
            this.ContactInforObservable = new ObservableCollection<OutletContactModel>();
            OutletContactModel outletContact = new OutletContactModel
            {
                CustomerKey = GlobalInformation.SelectedTCARCUS.Customerkey,
                FullName = "",
                Role = "",
                Gender = "",
                Email = "",
                Phone = "",
                Title = ""
            };
            this.ContactInforObservable.Add(outletContact);
        }

        private void GetOutletNokiaMerchandisingPotential()
        {
            this.GetOutletNokiaMerchandisingType();
            this.OutletNokiaMerchandisingPotentialObservable = new ObservableCollection<OutletNokiaMerchandisingPotentialExtend>();
            foreach (var item in this.OutletNokiaMerchandisingTypeObservable)
            {
                OutletNokiaMerchandisingPotentialExtend outletNokia = new OutletNokiaMerchandisingPotentialExtend
                {
                    ID_PK = Guid.NewGuid(),
                    Customerkey = GlobalInformation.SelectedTCARCUS.Customerkey,
                    MerchandisingCode = item.MerchandisingCode,
                    MerchandisingName = item.Description,
                };
                this.OutletNokiaMerchandisingPotentialObservable.Add(outletNokia);
            }
        }

        private void GetOutletNokiaPG()
        {
            this.GetNokiaPG();
            OutletNokiaPGObservable = new ObservableCollection<OutletNokiaPGExtend>();
            foreach (var item in this.NokiaPGObservable)
            {
                OutletNokiaPGExtend nokiaPG = new OutletNokiaPGExtend
                {
                    ID_PK = Guid.NewGuid(),
                    Customerkey = GlobalInformation.SelectedTCARCUS.Customerkey,
                    PGCode = item.PGCode,
                    PGName = item.PGName,
                    EndWorkingTime = DateTime.Now,
                    StartWorkingTime = DateTime.Now
                };
                this.OutletNokiaPGObservable.Add(nokiaPG);
            }
        }

        private void GetOutletPGInformation()
        {
            this.OutetPGInformationObservable = new ObservableCollection<OutletPGInformationExtend>();
            foreach (var item in this.PhoneVendorObservable)
            {
                OutletPGInformationExtend outletPG = new OutletPGInformationExtend
                {
                    ID_PK = Guid.NewGuid(),
                    Customerkey = GlobalInformation.SelectedTCARCUS.Customerkey,
                    PhoneVendorCode = item.PhoneVendorCode,
                    PhoneVendorName = item.PhoneVendorName,
                    Qty = 0
                };
                this.OutetPGInformationObservable.Add(outletPG);
            }
        }

        private void GetCurrentInDoorDoorDisplay()
        {
            this.CurrentInDoorDoorDisplayObservable = new ObservableCollection<CurrentInDoorDisplayExtend>();
            foreach (var item in this.PhoneVendorObservable)
            {
                foreach (var indoorType in this.InDoorTypeList)
                {
                    CurrentInDoorDisplayExtend currentIn = new CurrentInDoorDisplayExtend
                    {
                        ID_PK = Guid.NewGuid(),
                        CustomerKey = GlobalInformation.SelectedTCARCUS.Customerkey,
                        PhoneVendorCode = item.PhoneVendorCode,
                        InDoorTypeCode = indoorType.Key,
                        PhoneVendorName = item.PhoneVendorName,
                        InDoorTypeName = indoorType.Value,
                        Qty = "0"
                    };
                    this.CurrentInDoorDoorDisplayObservable.Add(currentIn);
                }

            }
        }

        private void GetCurrentOutDoorDisplay()
        {
            this.CurrentOutDoorDisplayObservable = new ObservableCollection<CurrentOutDoorDisplayExtend>();
            foreach (var item in this.PhoneVendorObservable)
            {
                foreach (var outDoorType in this.OutDoorTypeList)
                {
                    CurrentOutDoorDisplayExtend current = new CurrentOutDoorDisplayExtend
                    {
                        ID_PK = Guid.NewGuid(),
                        Customerkey = GlobalInformation.SelectedTCARCUS.Customerkey,
                        PhoneVendorCode = item.PhoneVendorCode,
                        PhoneVendorName = item.PhoneVendorName,
                        OutDoorTypeName = outDoorType.Value,
                        OutDoorTypeCode = outDoorType.Key,
                        Qty = 0,
                        Picture = string.Empty
                    };

                    if (SelectedOutDoorDisplay != null && SelectedOutDoorDisplay.PhoneVendorCode == item.PhoneVendorCode && SelectedOutDoorDisplay.OutDoorTypeCode == outDoorType.Value)
                    {
                        current.Picture = SelectedOutDoorDisplay.Picture;
                    }

                    this.CurrentOutDoorDisplayObservable.Add(current);
                }

            }
        }

        private void GetOutltVendorContribution()
        {
            this.GetOutletVender();
            this.OutletVendorContributionObserable = new ObservableCollection<OutltVendorContributionExtend>();
            foreach (var item in this.OutletVenderObservable)
            {
                OutltVendorContributionExtend outlet = new OutltVendorContributionExtend
                {
                    ID_PK = Guid.NewGuid(),
                    CustomerKey = GlobalInformation.SelectedTCARCUS.Customerkey,
                    OutletVendorKey = item.OutletVendorKey,
                    OutletVendorName = item.OutletVendorName,
                    ContributionAmt = 0
                };

                this.OutletVendorContributionObserable.Add(outlet);
            }
        }

        private void GetPhonevendorVolume()
        {
            this.PhonevendorVolumeObservable = new ObservableCollection<PhonevenforVolumeExtend>();

            foreach (var item in this.PhoneVendorObservable)
            {
                PhonevenforVolumeExtend volume = new PhonevenforVolumeExtend
                {
                    ID_PK = Guid.NewGuid(),
                    CustomerKey = GlobalInformation.SelectedTCARCUS.Customerkey,
                    PhoneVendorCode = item.PhoneVendorCode,
                    PhoneVendorName = item.PhoneVendorName,
                    RetailVolume = 0,
                    RetialValue = 0,
                    WhosesaleValue = 0,
                    WhosesaleVolume = 0
                };

                this.PhonevendorVolumeObservable.Add(volume);
            }
        }

        private void GetPriceRangeContribution()
        {
            this.GetPriceRangeList();
            this.PriceRangeContributionObservable = new ObservableCollection<PriceRangeContributionExtend>();
            foreach (var item in this.PriceRangeObservable)
            {
                PriceRangeContributionExtend price = new PriceRangeContributionExtend
                {
                    ID_PK = Guid.NewGuid(),
                    CustomerKey = GlobalInformation.SelectedTCARCUS.Customerkey,
                    ContributionByValue = 0,
                    ContributionByVolume = 0,
                    PriceRangeName = item.Descriptionn,
                    PriceRangeCode = item.PriceRangeCode
                };

                this.PriceRangeContributionObservable.Add(price);
            }
        }

        private void GetNokiaPG()
        {
            //this.NokiaPGObservable = new ObservableCollection<NokiaPG>(this._smartMobileContext.NokiaPG.ToList());
            this.NokiaPGObservable = new ObservableCollection<NokiaPG>
            {
                new NokiaPG{PGCode = "", ID_PK = Guid.NewGuid(), PGName = "Abc"}
            };
        }

        private void GetOutletVender()
        {
            this.OutletVenderObservable = new ObservableCollection<OutletVendor>(this._smartMobileContext.OutletVendor.ToList());
        }

        private void GetPhoneVender()
        {
            this.PhoneVendorObservable = new ObservableCollection<PhoneVendor>(this._smartMobileContext.PhoneVendor.Where(p => p.IsOperator == false).ToList());
        }

        private void GetPriceRangeList()
        {
            this.PriceRangeObservable = new ObservableCollection<PriceRange>(this._smartMobileContext.PriceRange.ToList());
        }

        private void GetOutletNokiaMerchandisingType()
        {
            this.OutletNokiaMerchandisingTypeObservable = new ObservableCollection<OutletNokiaMerchandisingType>(this._smartMobileContext.OutletNokiaMerchandisingType.ToList());
        }

        private bool? ConvertGender(string gender)
        {
            if (gender == "Male")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void GetOutletAdvanceInforListing() 
        {
            this.OutletAdvanceInforListingObservable = new ObservableCollection<OutletAdvanceInforListing>(this._smartMobileContext.OutletAdvanceInforListing.Where(a => a.IsShow == true).OrderBy(a => a.ShowOrder).ToList());
        }

        private void GetOutletAdvanceInforTableObservable() 
        {
            this.OutletAdvanceInforTableObservable = new ObservableCollection<OutletAdvanceInforTable>(this._smartMobileContext.OutletAdvanceInforTable.ToList());
        }

        public void BindingFlexgrid(OutletAdvanceInforListing table) 
        {
            switch (table.InforName.ToString())
            {
                case "PhonevendorVolume":
                    {
                        List<OutletAdvanceInforTable> advanceInforTableList = this.OutletAdvanceInforTableObservable.Where(a => a.DataTable == table.DataTable).OrderBy(a => a.FieldShow).ToList();
                        foreach (var advanceInforTable in advanceInforTableList)
                        {
                            
                        }

                        break;
                    }                    
                case "PriceRangeContribution":                    
                    break;
                default:
                    break;
            }
        }
    }
}
